﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using KhatVongViet.Models;
using PagedList;

namespace KhatVongViet.Controllers
{
    public class HomeController : Controller
    {
        KhatVongVietEntities dataContext = new KhatVongVietEntities();
        GetData getData = new GetData();

        public ActionResult Default()
        {
            var model = new ArticleCustom();

            List<Article> listSlideShow = getData.GetListSlideShow();
            model.ListSlideShow = listSlideShow;

            List<Article> listArticle = getData.GetNewsArticle();
            model.ListNewsArticles = listArticle;

            List<Article> listHoatDong = getData.GetActivitiesArticles();
            model.ListActivities = listHoatDong;

            List<Article> listThongBao = getData.GetNoticesArticles();
            model.ListNotices = listThongBao;

            List<Article> listCamXucViet = getData.GetTopCamXuc();
            model.ListConfession = listCamXucViet;

            return View(model);
        }

        [HttpGet]
        public ActionResult DetailArticle(int idArticle)
        {
            //Update viewer.
            var article = dataContext.Articles.Single(p => p.ID == idArticle);
            article.ViewNumber += 1;
            dataContext.SaveChanges();

            var model = new ArticleCustom
            {
                DetailArticle = getData.GetArticle(idArticle),
                ListOldArticle = getData.GetListOldArticle(idArticle),
                ListNewArticle = getData.GetListNewArticle(idArticle),
                ListOtherArticle = getData.GetListOtherArticle(idArticle)
            };
            return View(model);
        }

        public ActionResult AllDetail(int page = 1)
        {
            List<Article> listAllCamXucViet = getData.GetAllDetail();
            const int numberitemperpage = 5;
            ViewBag.listAllCamXucViet = listAllCamXucViet;
            ViewBag.Page = page;
            ViewBag.NumberItemPerPage = 5;
            ViewBag.NumberArticle = listAllCamXucViet.Count();
            return View(listAllCamXucViet.OrderBy(v => v.ID).ToPagedList(page, numberitemperpage));
        }

        public ActionResult AllDetailNews(int page = 1)
        {
            List<Article> listAllCamXucViet = getData.GetAllDetailNews();
            const int numberitemperpage = 5;
            ViewBag.listAllCamXucViet = listAllCamXucViet;
            ViewBag.Page = page;
            ViewBag.NumberItemPerPage = 5;
            ViewBag.NumberArticle = listAllCamXucViet.Count();
            return View(listAllCamXucViet.OrderBy(v => v.ID).ToPagedList(page, numberitemperpage));
        }

        public ActionResult AllDetailActivities(int page = 1)
        {
            List<Article> listAllCamXucViet = getData.GetAllDetailActivities();
            const int numberitemperpage = 5;
            ViewBag.listAllCamXucViet = listAllCamXucViet;
            ViewBag.Page = page;
            ViewBag.NumberItemPerPage = 5;
            ViewBag.NumberArticle = listAllCamXucViet.Count();
            return View(listAllCamXucViet.OrderBy(v => v.ID).ToPagedList(page, numberitemperpage));
        }

        public ActionResult AllDetailConfession(int page = 1)
        {
            List<Article> listAllCamXucViet = getData.GetAllDetailConfession();
            const int numberitemperpage = 5;
            ViewBag.listAllCamXucViet = listAllCamXucViet;
            ViewBag.Page = page;
            ViewBag.NumberItemPerPage = 5;
            ViewBag.NumberArticle = listAllCamXucViet.Count();
            return View(listAllCamXucViet.OrderBy(v => v.ID).ToPagedList(page, numberitemperpage));
        }

        public ActionResult AllDetailNotice(int page = 1)
        {
            List<Article> listAllCamXucViet = getData.GetAllDetailNotice();
            const int numberitemperpage = 5;
            ViewBag.listAllCamXucViet = listAllCamXucViet;
            ViewBag.Page = page;
            ViewBag.NumberItemPerPage = 5;
            ViewBag.NumberArticle = listAllCamXucViet.Count();
            return View(listAllCamXucViet.OrderBy(v => v.ID).ToPagedList(page, numberitemperpage));
        }
    }
}